package com.heyqing.ems.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heyqing.ems.common.MemberType;
import com.heyqing.ems.domain.BossInfo;
import com.heyqing.ems.domain.ResignationManagement;
import com.heyqing.ems.mapper.EmployeeInfoMapper;
import com.heyqing.ems.mapper.ResignationManagementMapper;
import com.heyqing.ems.po.QueryPo;
import com.heyqing.ems.po.ResignationPo;
import com.heyqing.ems.service.BossService;
import com.heyqing.ems.service.EmployeeService;
import com.heyqing.ems.service.ManagerService;
import com.heyqing.ems.service.ResignationService;
import com.heyqing.ems.vo.ResignationVo;
import com.heyqing.ems.vo.Result;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * ClassName:ResignationServiceImpl
 * Package:com.heyqing.ems.service.impl
 * Description:
 *
 * @Date:2024/6/26
 * @Author:Heyqing
 */
@Service
@RequiredArgsConstructor
public class ResignationServiceImpl extends ServiceImpl<ResignationManagementMapper, ResignationManagement> implements ResignationService {

    private final BossService bossService;
    private final EmployeeService employeeService;
    private final ManagerService managerService;

    @Override
    public Result getList(QueryPo queryPo) {
        if (Objects.isNull(queryPo)){
            return Result.fail();
        }
        Page<ResignationManagement> resPage = lambdaQuery()
                .page(new Page<ResignationManagement>(queryPo.getPageNum(), queryPo.getPageSize()));
        if (Objects.isNull(resPage) || Objects.isNull(resPage.getRecords()) || Objects.isNull(resPage.getTotal())){
            return Result.fail();
        }
        Map<String,Object> map = new HashMap<>();
        List<ResignationManagement> records = resPage.getRecords();
        List<ResignationVo> res= copy2Vo(records);
        map.put("list",res);
        map.put("total",resPage.getTotal());
        return Result.success(map);
    }

    @Override
    @Transactional
    public Result saveResignation(ResignationPo resignationPo) {
        ResignationManagement resignationManagement = new ResignationManagement();
        BeanUtil.copyProperties(resignationPo,resignationManagement);
        saveOrUpdate(resignationManagement);
        return Result.success();
    }

    @Override
    public Result agree(Long id) {
        ResignationManagement rm = getById(id);
        if (Objects.isNull(rm)){
            return Result.fail();
        }
        String t = String.valueOf(rm.getType());
        Long Id = rm.getAttendanceId();
        if (t.equals(MemberType.EMPLOYEE)){
            Result delete = managerService.delete(Id);
            if (delete.getMate().getCode() != 200){
                return Result.fail();
            }
        }else {
            Result delete = bossService.delete(Id);
            if (delete.getMate().getCode() != 200){
                return Result.fail();
            }
        }
        return Result.success();
    }

    /***************************************private****************************/
    private ResignationVo copy(ResignationManagement info){
        ResignationVo resignationVo = new ResignationVo();
        BeanUtil.copyProperties(info,resignationVo);
        String  t = String.valueOf(info.getType());
        String type = "";
        String name = "";
        Long id = info.getAttendanceId();
        if (t.equals(MemberType.EMPLOYEE)){
            type = "员工";
            name = employeeService.getById(id).getName();
        }else {
            type = "经理";
            name = managerService.getById(id).getName();
        }

        resignationVo.setName(name);
        resignationVo.setType(type);
        return resignationVo;
    }
    private List<ResignationVo> copy2Vo(List<ResignationManagement> records) {
        List<ResignationVo> res = new ArrayList<>();
        for (ResignationManagement record : records) {
            res.add(copy(record));
        }
        return res;
    }

}
